﻿using System.Collections.Generic;
using System.Linq;
using System.ComponentModel;
using System.Web.Mvc;
using System.Text;
using System.Configuration;

namespace Bids.Core.Common.Net
{
    public static class UrlHandler
    {
        /// <summary>
        /// Convert raw query string to NameValueCollection
        /// </summary>
        /// <param name="s">raw query string</param>
        static string ConvertQueryStringToCollection(System.Web.HttpRequestBase Request, object FilterKey)
        {


            string Url = Request.RawUrl.ToString();
            if (FilterKey == null && !Request.QueryString.HasKeys())
                return Url;
            if (!Url.Contains("?"))
                Url = Url + "?";
            else
                Url = Url.Substring(0, Url.IndexOf("?") + 1);

            var rq = Request.QueryString;

            StringBuilder QueryParam = new StringBuilder();

            foreach (string key in rq)
            {
                if (key == null)
                    continue;
                foreach (PropertyDescriptor descriptor in TypeDescriptor.GetProperties(FilterKey))
                {
                    if (descriptor.Name.ToUpper() == key.ToUpper())
                        goto next;
                }
                QueryParam.Append("&");
                QueryParam.Append(key);
                QueryParam.Append("=");
                QueryParam.Append(rq[key]);

            next: ;

            }
            foreach (PropertyDescriptor descriptor in TypeDescriptor.GetProperties(FilterKey))
            {
                QueryParam.Append("&");
                QueryParam.Append(descriptor.Name);
                QueryParam.Append("=");
                QueryParam.Append(descriptor.GetValue(FilterKey).ToString());
            }
            return Url + QueryParam.Remove(0, 1).ToString();
        }

        #region
        /// <summary>
        /// 通过传入匿名对象对url参数进行过滤或替换
        /// </summary>
        /// <param name="Page">WebForm对象</param>
        /// <param name="Filter">传入要过滤的对象,匿名对象用new { }创建</param>
        /// <returns></returns>
        public static string GetUrl(this System.Web.HttpRequestBase Request, object Filter)
        {
            return ConvertQueryStringToCollection(Request, Filter);
        }
        /// <summary>
        /// 获取url
        /// </summary>
        /// <param name="Page">WebForm对象</param>
        /// <param name="Filter"></param>
        /// <returns></returns>
        public static string GetUrl(this System.Web.HttpRequestBase Request)
        {
            return ConvertQueryStringToCollection(Request, null);
        }
        public static string GetUrlParamString(this System.Web.HttpRequestBase Request)
        {
            if (Request.Url.ToString().IndexOf("?") < 0)
                return "";
            return Request.Url.ToString().Substring(Request.Url.ToString().IndexOf("?"));
        }

        #endregion
    }
}
